#!/usr/bin/env python
# -*- coding:utf-8 -*-
# __author__ = 'Yunchao Ling'

import sys

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)

infile = open("D:/data/2017springdown/undown.txt", "r")

outfile1 = open("D:/data/2017springdown/undown_wget.sh", "w")
outfile1.write("#!/bin/sh\n\n")
outfile1.flush()

outfile2 = open("D:/data/2017springdown/undown_ascp.sh", "w")
outfile2.write("#!/bin/sh\n\n")
outfile2.flush()

for line in infile:
    line = line.rstrip()
    if line != "":
        id = line
        prefix1 = line[:3]
        prefix2 = line[:6]
        outfile1.write("echo 'Start downloading " + id + "'\n")
        outfile1.write(
            "wget -nv ftp://ftp.ncbi.nlm.nih.gov/sra/sra-instant/reads/ByRun/sra/" + prefix1 + "/" + prefix2 + "/" + id + "/" + id + ".sra\n")
        outfile1.flush()

        outfile2.write("echo 'Start downloading " + id + "'\n")
        outfile2.write(
            "ascp -i ~/.aspera/connect/etc/asperaweb_id_dsa.openssh -Tr -Q -l200M anonftp@ftp-private.ncbi.nlm.nih.gov:/sra/sra-instant/reads/ByRun/sra/" + prefix1 + "/" + prefix2 + "/" + id + "/" + id + ".sra ./\n")
        outfile2.flush()

infile.close()
outfile1.close()
outfile2.close()
